# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/11/27
import traceback

from handlers.common.lib.log_ import logger


def format_bounds_str_to_list(bounds_str):
    '''

    :param bounds_str:"[596,583][680,620]"
    :return:[596, 583, 680, 620]
    '''
    # logger.info("bounds_str:%s" % bounds_str)
    data = bounds_str.replace(" ", "").strip("[").strip("]").split("][")
    for i in range(len(data)):
        tmp_list = data[i].split(",")
        data[i] = tmp_list
    res = [int(data[0][0]), int(data[0][1]), int(data[1][0]), int(data[1][1])]
    return res


def format_to_list_bounds_str(bounds_list):
    '''

    :param bounds_list:[596, 583, 680, 620]
    :return:"[596,583][680,620]"
    '''
    # logger.info("bounds_list:%s" % str(bounds_list))
    res = "[%d,%d][%d,%d]" % (bounds_list[0], bounds_list[1], bounds_list[2], bounds_list[3])
    return res


def format_bounds_str_to_list_plus(bounds_str):
    '''

    :param bounds_str:     "[83,678][188,766]"
    :return:     (396, 1601, 666, 1668)
    '''
    data = bounds_str.replace(" ", "").strip("[").strip("]").split("][")
    for i in range(len(data)):
        tmp_list = data[i].split(",")
        data[i] = tmp_list
    width = int((int(data[1][0]) - int(data[0][0])) * 1)
    hight = int((int(data[1][1]) - int(data[0][1])) * 1)
    # logger.info("width%d" % width)
    # logger.info("hight%d" % hight)
    res = [int(data[0][0]) - width, int(data[0][1]) - hight, int(data[1][0]) + width, int(data[1][1]) + hight]
    return res


def format_bounds_to_positions(bounds, easy_path):
    '''
    格式化从appium_destop复制的bounds数据供tap()使用
    :param positions(str):"[xxx,xxx][xxx,xxx]"
    :return positions:[(xxx,xxx), (xxx,xxx)]
    '''
    try:
        data = bounds.replace(" ", "").strip("[").strip("]").split("][")
        for i in range(len(data)):
            tmp_list = data[i].split(",")
            data[i] = tuple(tmp_list)
        x = int((int(data[0][0]) + int(data[1][0])) / 2)
        y = int((int(data[0][1]) + int(data[1][1])) / 2)
        positions = [(x, y)]
        logger.info("positions:%s" % str(positions))
    except Exception as e:
        logger.error(repr(e))
        logger.error("%s的bounds值格式错误" % easy_path)
        logger.error(traceback.format_exc())
    return positions


def format_positions(bounds, easy_path):
    '''
    格式化数据供tap()使用
    :param positions(str):"[xxx,xxx][xxx,xxx]"
    :return positions:[(xxx,xxx), (xxx,xxx)]
    '''
    try:
        data = bounds.replace(" ", "").strip("[").strip("]").split("][")
        positions = []
        for i in range(len(data)):
            tmp_list = data[i].split(",")
            data[i] = tuple(tmp_list)
            x = int(data[i][0])
            y = int(data[i][1])
            positions.append((x, y))
        logger.info("positions:%s" % str(positions))
    except Exception as e:
        logger.error(repr(e))
        logger.error("%s的bounds值格式错误" % easy_path)
        logger.error(traceback.format_exc())
    return positions


if __name__ == "__main__":
    s = "[184,10][1050,50]"
    res = format_bounds_str_to_list(s)
    print(res)
